package com.ruoyi.official.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.official.domain.OfficialCategory;
import com.ruoyi.official.domain.OfficialProduct;
import com.ruoyi.official.mapper.OfficialCategoryMapper;
import com.ruoyi.official.mapper.OfficialProductMapper;
import com.ruoyi.official.service.IOfficialCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类Service业务层处理
 *
 * @author yuanqiji
 * @date 2025-03-21
 */
@Service
public class OfficialCategoryServiceImpl implements IOfficialCategoryService {
    @Autowired
    private OfficialCategoryMapper officialCategoryMapper;
    @Autowired
    private OfficialProductMapper officialProductMapper;

    /**
     * 查询分类
     *
     * @param id 分类主键
     * @return 分类
     */
    @Override
    public OfficialCategory selectOfficialCategoryById(Long id) {
        return officialCategoryMapper.selectOfficialCategoryById(id);
    }

    /**
     * 查询分类列表
     *
     * @param officialCategory 分类
     * @return 分类
     */
    @Override
    public List<OfficialCategory> selectOfficialCategoryList(OfficialCategory officialCategory) {
        return officialCategoryMapper.selectOfficialCategoryList(officialCategory);
    }

    @Override
    public List<OfficialCategory> selectOfficialAll() {
        return officialCategoryMapper.selectOfficialAll();
    }

    /**
     * 新增分类
     *
     * @param officialCategory 分类
     * @return 结果
     */
    @Override
    public int insertOfficialCategory(OfficialCategory officialCategory) {
        officialCategory.setCreateTime(DateUtils.getNowDate());
        return officialCategoryMapper.insertOfficialCategory(officialCategory);
    }

    /**
     * 修改分类
     *
     * @param officialCategory 分类
     * @return 结果
     */
    @Override
    public int updateOfficialCategory(OfficialCategory officialCategory) {
        officialCategory.setUpdateTime(DateUtils.getNowDate());
        return officialCategoryMapper.updateOfficialCategory(officialCategory);
    }

    /**
     * 批量删除分类
     *
     * @param ids 需要删除的分类主键
     * @return 结果
     */
    @Override
    public int deleteOfficialCategoryByIds(Long[] ids) {


        return officialCategoryMapper.deleteOfficialCategoryByIds(ids);
    }

    /**
     * 删除分类信息
     *
     * @param id 分类主键
     * @return 结果
     */
    @Override
    public int deleteOfficialCategoryById(Long id) {
        return officialCategoryMapper.deleteOfficialCategoryById(id);
    }

    @Override
    public int selectOfficialProductList(Long[] ids) {
        int number = 0;
        for (Long id : ids) {
            OfficialProduct officialProduct = new OfficialProduct();
            officialProduct.setCategoryId(id);
            List<OfficialProduct> officialProducts = officialProductMapper.selectOfficialProductList(officialProduct);
            number = number + officialProducts.size();
        }
        return number;
    }
}
